package com.company;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import okhttp3.Call;
import org.junit.Test;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;
import java.util.Random;
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Main {
    private  static final Logger logger = LoggerFactory.getLogger(Main.class.getName());
    private  static final String TAG = "MAIN";
    static {
        Properties prop = new Properties();

        try {
            InputStream instream = SequenceInputStream.class.getResourceAsStream("/conf.properties");//测试环境
//            FileInputStream fileInputStream = new FileInputStream(new File("C:\\Users\\Administrator\\IdeaProjects\\monitor\\src\\email.properties"));
            prop.load(instream);//测试环境
        } catch (IOException e) {
            logger.error(TAG,e);
        }
        String dex_version = prop.getProperty("dex_version");
        Utils.DEX_VERSION = dex_version;
        logger.info("读取配置dexversion:  "+ dex_version);
    }

    private static Timer timer;

    public static MessageDigest mMessageDigest = null;



    public static void main(String[] args) {
        logger.info("Main run... ");
        try {
            mMessageDigest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            logger.error( TAG,e);
        }
        if (mMessageDigest==null){
            return;
        }

//        Utils.isSmaeFile("js/ad.trunk1.js","js/ad.trunk.js",mMessageDigest);
//        Utils.isSmaeFile("js/sdk.ios1.js","js/sdk.ios.js",mMessageDigest);
//        List<String> strings=new ArrayList<>();
//        strings.add("js/c.js");
//        strings.add("js/c1.js");
//        SendEmailUtil sendEmailUtil = new SendEmailUtil("993192645@qq.com","警报 警报.....","js文件发生变化了",strings,1 );
//        sendEmailUtil.send();
//        boolean smaeFile = Utils.isSmaeFile("js/c.js", "js/c1.js", mMessageDigest);
//        System.out.println(smaeFile);

        File   c = new File(Utils.basedir + "c.js");
        if (!c.exists()) {
            logger.info("file  not  exit donwload  js  file...");
            Utils.getC_JS(RemindTask.c_url, "c.js");
            Utils.getC_JS(RemindTask.ad_trunk_url, "ad.trunk.js");
            Utils.getC_JS(RemindTask.sdk_ios_url, "sdk.ios.js");
        }else{
            logger.info("js file  exits dont download...");
        }

        startSchedule();

//           new Thread( new WorkerThread(mMessageDigest)).start();
//        ScheduledExecutorService executor = Executors.newScheduledThreadPool(4);
//        executor.scheduleWithFixedDelay(new RemindTask(mMessageDigest),60*1000,1*60*60*1000,TimeUnit.MILLISECONDS);
////        timer = new Timer();
////      timer.schedule(new RemindTask(mMessageDigest), 3*60*60*1000,3*60*60*1000);
////        timer.scheduleAtFixedRate(new RemindTask(mMessageDigest), 60*1000,1*60*60*1000);
//        logger.info("timer  schedule...");
    }


    @Test
    public void ad(){
        System.out.printf("null instanceof Integer?  "+(null instanceof Integer));
    }

    /**
     * 开始一个simpleSchedule()调度
     */
    public static void startSchedule() {
        try {
            // 1、创建一个JobDetail实例，指定Quartz
            JobDetail jobDetail = JobBuilder.newJob(RemindTask.class)
                    // 任务执行类
                    .withIdentity("job1_1", "jGroup1")
                    // 任务名，任务组
                    .build();


            //触发器类型
            SimpleScheduleBuilder builder = SimpleScheduleBuilder
                    // 设置执行次数
                    .repeatHourlyForever();

            //CronScheduleBuilder builder = CronScheduleBuilder.cronSchedule("0/2 * * * * ?");
            // 2、创建Trigger
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("trigger1_1", "tGroup1").startNow()
                    .withSchedule(builder)
                    .build();

            // 3、创建Scheduler
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();
            // 4、调度执行
            scheduler.scheduleJob(jobDetail, trigger);

        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
